package com.partner.boot.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.partner.boot.common.Constants;
import com.partner.boot.common.Result;
import com.partner.boot.controller.domain.UserRequest;
import com.partner.boot.entity.User;
import com.partner.boot.service.IUserService;
import com.partner.boot.utils.EmailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

@Api(tags = "无权限接口列表")
@RestController
@Slf4j
public class WebController {

    @Resource
    private IUserService iUserService;

    @Autowired
    private EmailUtils emailUtils;

    /**
     * @description: 默认访问接口
     * @Param: []
     * @return: String
     * @author 刘孟坤
     * @date: 2023/7/13 21:47
     */
    @GetMapping(value = "/")
    @ApiOperation(value = "版本校验")
    public Result version() {
        // 应用版本号
        String ver = "partner-back-0.0.1-SNAPSHOT";
        Package aPackage = WebController.class.getPackage();
        String title = aPackage.getImplementationTitle();
        String version = aPackage.getImplementationVersion();
        return Result.success();
    }

    /**
     * @param user
     * @return com.partner.boot.common.Result
     * @Version 1.0
     * @description TODO
     * @author 刘孟坤
     * @date 2023/7/31 1:07
     */
    @ApiOperation(value = "用户登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody UserRequest user) {
        long start = System.currentTimeMillis();
        User login = iUserService.login(user);
        log.info("登录花费时间:{}", System.currentTimeMillis() - start);
        return Result.success(login);
    }

    /**
     * @param user
     * @return com.partner.boot.common.Result
     * @Version 1.0
     * @description 用户注册接口
     * @author 刘孟坤
     * @date 2023/7/31 22:59
     */
    @ApiOperation(value = "用户注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody UserRequest user) {
        User res = iUserService.register(user);
        return Result.success(res);
    }

    /**
     * @description: 邮箱验证接口
     * @author 刘孟坤
     * @date: 2023/8/3 22:02
     * @param: user
     * @return: com.partner.boot.common.Result
     */
    @ApiOperation(value = "邮箱验证接口")
    @GetMapping("/emailregister")
    public Result email(@RequestParam String email, @RequestParam String type) {
        log.info("email:{},type:{}", email, type);
        iUserService.sendEmail(email, type);
        return Result.success();
    }


    @ApiOperation(value = "密码重置接口")
    @PostMapping("/password/reset")
    public Result resetPassword(@RequestBody UserRequest userRequest) {
        String newPassword = iUserService.resetPassword(userRequest);
        return Result.success(newPassword);
    }

    @ApiOperation(value = "用于优化登录")
    @GetMapping("/goodfirstlogin")
    public Result goodFirstLogin(@RequestParam String username) {
        User testLogin = iUserService.goodFirstLogin(username);
        return Result.success(testLogin);
    }
}
